#!/bin/groovy
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the terms found in the LICENSE file in the root of this
 * source tree.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */


def GIT_URL = "github.com/magma/magma"

def GIT_BRANCH = "master"
def GIT_COMMIT

def OAI_GIT_URL = "https://github.com/OPENAIRINTERFACE/openair-epc-fed.git"
def OAI_GIT_BRANCH = "master"
// Location of the executor node
def nodeExecutor = "libvirt"

def slack_channel = "#magma-ci-bot"
// lock mechanism
def cn_ci_resource = params.MagmaVmDockerResources


pipeline {
  agent {
    label "libvirt"
  }
  options {
    timestamps()
    ansiColor('xterm')
  }

  stages {
    stage ("Verify Parameters") {
      steps {
        script {
          cleanWs()
          JOB_TIMESTAMP = sh returnStdout: true, script: 'date --utc --rfc-3339=seconds | sed -e "s#+00:00##"'
          JOB_TIMESTAMP = JOB_TIMESTAMP.trim()

          echo '\u2705 \u001B[32mVerify Parameters\u001B[0m'
        }
      }
    }
    stage ("Retrieve and Prepare Source Code") {
      steps {
        script {
          checkout(
            changelog: false,
            poll: false,
            scm: [$class: 'GitSCM',
                  branches: [[name: '$sha1']],
                  doGenerateSubmoduleConfigurations: false,
                  extensions: [],
                  submoduleCfg: [],
                  userRemoteConfigs: [[refspec: '+refs/pull/*:refs/remotes/origin/pr/*', url: "https://" + GIT_URL + ".git"]]]
          )
          sh "git clean -x -d -e .cache -e lte/gateway/.vagrant -f > /dev/null 2>&1"
          sh("mkdir -p openair-epc-fed archives")
          dir("openair-epc-fed") {
            checkout(
                changelog: false,
                poll: false,
                scm: [$class: 'GitSCM',
                      branches: [[name: OAI_GIT_BRANCH]],
                      doGenerateSubmoduleConfigurations: false,
                      doGenerateSubmoduleConfigurations: false,
                      extensions: [[$class: 'SubmoduleOption',
                                    disableSubmodules: false,
                                    parentCredentials: false,
                                    recursiveSubmodules: true,
                                    reference: '',
                                    trackingSubmodules: false]],
                      submoduleCfg: [],
                      userRemoteConfigs: [[url: OAI_GIT_URL]]
                ]
            )
          }
        }
      }
      post {
        failure {
          script {
            def message = "OAI " + JOB_NAME + " build (" + BUILD_ID + "): Merge Conflicts -- Cannot perform CI"
            echo message
            currentBuild.result = 'FAILURE'
          }
        }
      }
    }
    stage ("Provisioning") {
      parallel {
        stage ("Provision the AGW VM") {
          steps {
            script {
              try {
                sh('sudo virsh list --all --name')
                sh('sudo virsh list --all --name | grep _magma | xargs --no-run-if-empty -n1 sudo virsh undefine')
                sh('cd lte/gateway;virsh undefine gateway_magma || true; vagrant destroy --force magma')
              }
              catch (Exception e) {
                  echo "Fine. Let it go..."
              }
              sh('cd lte/gateway && vagrant up --provider libvirt magma')
              // Check that magma services are all down. Should be the case after wake-up
              try {
                sh('cd lte/gateway && vagrant ssh magma -c "sudo service magma@* status"')
              } catch (Exception e) {
                echo "Fine. Let it go..."
              }
              sh('which zip || sudo apt-get install -y zip')
              sh('dpkg -l apt-utils || apt-get install -y apt-utils')
            }
          }
        }
        stage ("Build Orchestrator") {
          steps {
            script {
              echo "Not building orc8r at the moment"
            }
          }
        }
      }
    }
    stage ("Building") {
      parallel {
        stage ("Build AGW1 - noS11") {
          steps {
            script {
                // Manual removal of build dirs
                try {
                  sh('cd lte/gateway && vagrant ssh magma -c "sudo rm -Rf build/c build/python"')
                } catch (Exception e) {
                  echo "OK after a git clean..."
                }
                try {
                  sh ('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make clean"')
                } catch (Exception e) {
                  echo "OK after a git clean..."
                }
                // Manually creating the c build dir
                try {
                  sh ('cd lte/gateway && vagrant ssh magma -c "mkdir -p build/c"' )
                } catch (Exception e) {
                  echo "It should not fail here but we still go on"
                }
                timeout (time: 120, unit: 'MINUTES') {
                  // removing the magma/.cache/gateway folder will slow down build from 3 minutes to 27 minutes
                  sh('''cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make run "''')
                }
                sh "sleep 30"
                // check magma status --> non-blocking (even if OK it might fail from a bash script point of view)
                try {
                    sh('cd lte/gateway && vagrant ssh magma -c "sudo service magma@* start; sudo service magma@* status"')
                } catch (Exception e) {
                  echo "Checking magma@* status failed but still moving on!"
                }
            }
          }
        }
        // Running CPPCHECK in parallel to gain time
        stage ('Static Code Analysis') {
          steps {
            script {
              // Running on xenial to have 1.72 version of cppcheck
              sh('docker rm -f ci-cn-cppcheck || true')
              sh('docker run --name ci-cn-cppcheck -v `pwd`:/code -d ubuntu:xenial /bin/bash -c "sleep infinity"')
              sh('docker exec -i ci-cn-cppcheck /bin/bash -c "apt-get update && apt-get upgrade --yes" 2>&1 > archives/cppcheck_install.log')
              sh('docker exec -i ci-cn-cppcheck /bin/bash -c "apt-get install --yes git cppcheck bzip2" 2>&1 >> archives/cppcheck_install.log')

              sh('docker exec -i ci-cn-cppcheck /bin/bash -c "cd /code && cppcheck -j8 --enable=warning --force --xml --xml-version=2 -i test ." 2> cppcheck.xml 1> cppcheck_build.log')
              sh('docker rm -f ci-cn-cppcheck')
            }
          }
          post {
            success {
              sh "echo 'CPPCHECK: OK' >> archives/cppcheck_install.log"
            }
            unsuccessful {
              sh "echo 'CPPCHECK: KO' >> archives/cppcheck_install.log"
            }
          }
        }
        stage ('Code Formatting Checker') {
          steps {
            script {
              sh('docker rm -f ci-cn-clang-formatter || true')
              sh('docker run --name ci-cn-clang-formatter -v `pwd`:/code -d ubuntu:bionic /bin/bash -c "sleep infinity"')
              sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "apt-get update && apt-get upgrade --yes" 2>&1 > archives/clang_format_install.log')
              sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "apt-get install --yes git tree bzip2" 2>&1 >> archives/clang_format_install.log')

              //sh('docker cp /tmp/converged_mme.tar.bz2 ci-cn-clang-formatter:/home')
              //sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "cd /home && tar -xjf converged_mme.tar.bz2"')
              //sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "rm -f /home/converged_mme.tar.bz2"')
              //sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "cd /home && git checkout -f ' + TEMP_COMMIT + '"')

              // We install a dedicated version (installed on our CI server).
              sh('docker cp /opt/clang-format/9.0.0/bin/clang-format ci-cn-clang-formatter:/usr/local/bin')
              sh('docker exec -i ci-cn-clang-formatter /bin/bash -c "cd /code && ./ci-scripts/checkCodingFormattingRules.sh"')
            }
          }
          post {
            always {
              script {
                sh('docker cp ci-cn-clang-formatter:/code/oai_rules_result.txt archives/.')
                // May not have been generated
                try {
                  sh('docker cp ci-cn-clang-formatter:/code/oai_rules_result_list.txt archives/.')
                } catch (Exception e) {
                  echo "Failed to copy src/oai_rules_result_list.txt! It may not have been generated. That's OK!"
                }
                // no need to keep the clang-formatter container
                sh('docker rm -f ci-cn-clang-formatter')
              }
            }
          }
        }
        stage ("Run Orchestrator") {
          steps {
            script {
              echo "Not at the moment"
            }
          }
        }
        stage ("Provision the Test VM") {
          steps {
            script {
                try {
                  sh('cd lte/gateway && virsh undefine gateway_magma_test || true; vagrant destroy --force magma_test')
                }
                catch (Exception e) {
                  echo "Fine. Let it go..."
                }
                sh('cd lte/gateway && vagrant up --provider libvirt magma_test')
                sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/ && make"')
                sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && make"')
            }
          }
        }
        stage ("Provision the Traffic VM") {
          steps {
            script {
              sh "sleep 60"
                try {
                  sh('cd lte/gateway && virsh undefine gateway_magma_trfserver || true; vagrant destroy --force magma_trfserver')
                } catch (Exception e) {
                  echo "Ignoring issues cleaning up any lingering magma_trfserver"
                }
                sh('cd lte/gateway && vagrant up --provider libvirt magma_trfserver')
                try {
                  sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo apt update"')
                } catch (Exception e) {
                  echo "Known issue with magma-custom.io public key?"
                }
                sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo apt install --yes psmisc net-tools iproute"')
                sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo ip route add 192.168.128.0/24 via 192.168.129.1 dev eth2"')
            }
          }
        }
      }
    }
    stage ("Testing -- noS11") {
      parallel {
        stage ("Start Traffic Server -- noS11") {
          steps {
            script {
              echo "Disabling TCP checksumming on Traffic VM"
                sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo ethtool --offload eth1 rx off tx off && sudo ethtool --offload eth2 rx off tx off"')
                echo "Starting the Traffic server in foreground"
                try {
                  sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo traffic_server.py 192.168.60.144 62462"')
                } catch (Exception e) {
                  echo "Moving on!"
                }
            }
          }
        }
        stage ("Test-AGW1-noS11") {
          steps {
            script {
                echo "Disabling TCP checksumming on all VMs"
                sh('cd lte/gateway && vagrant ssh magma -c "sudo ethtool --offload eth1 rx off tx off && sudo ethtool --offload eth2 rx off tx off"')
                sh('cd lte/gateway && vagrant ssh magma_test -c "sudo ethtool --offload eth1 rx off tx off && sudo ethtool --offload eth2 rx off tx off"')

                // Making sure the Traffic server is up and running
                sh "sleep 20"

                echo "Starting the integration Tests - S1AP Tester"
                // We have removed the traffic testcases from mandatory suite.

          // FIXME!!!! set 110 MINUTES instead of 30

                timeout (time: 110, unit: 'MINUTES') {
                  sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test"')
                }

                timeout (time: 45, unit: 'SECONDS') {
                  try {
                    sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_dl_udp_data.py"')
                  } catch (Exception e) {
                    echo "s1aptests/test_attach_dl_udp_data testcase may fail"
                  }
                }
                timeout (time: 45, unit: 'SECONDS') {
                  try {
                    sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_dl_tcp_data.py"')
                  } catch (Exception e) {
                    echo "s1aptests/test_attach_dl_tcp_data testcase may fail"
                  }
                }

                echo "Stopping the Traffic server in background"
                try {
                  sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo killall python3"')
                } catch (Exception e) {
                  echo "Maybe Traffic server crashed"
                }
            }
          }
          post {
            always {
              script {
                def retrieveOAIcovFiles = true
                try {
                  sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make coverage_oai"')
                } catch (Exception e) {
                  echo "Let's keep running to have some logs, but not the OAI coverage files"
                  retrieveOAIcovFiles = false
                }
                if (retrieveOAIcovFiles) {
                  try {
                    sh('cd lte/gateway/c/oai && zip -r -qq ${WORKSPACE}/archives/code_coverage.zip code_coverage/')
                  } catch (Exception e) {
                    echo "Maybe we could not generate the coverage HTML report"
                  }
                }
                sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make stop"')
                // Retrieving the sys logs and mme log for more debugging.
                sh('cd lte/gateway && vagrant ssh magma -c "sudo cat /var/log/syslog" > ${WORKSPACE}/archives/magma_dev_syslog.log')
                sh('cd lte/gateway && vagrant ssh magma -c "sudo cat /var/log/mme.log" > ${WORKSPACE}/archives/magma_dev_mme.log')
                sh('cd lte/gateway && vagrant ssh magma_test -c "sudo cat /var/log/syslog" > ${WORKSPACE}/archives/magma_test_syslog.log')
              }
            }
            success {
              sh "echo 'AGW-VM-S1AP-TESTS: OK' >> archives/magma_run_s1ap_tester.log"
            }
            unsuccessful {
              script {
                try {
                  sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo killall python3"')
                } catch (Exception e) {
                  echo "Why it fails to kill the traffic server?"
                }
                sh "echo 'AGW-VM-S1AP-TESTS: KO' >> archives/magma_run_s1ap_tester.log"
              }
            }
          }
        }
      }
    }
stage ("Re-Build MME-S11") {
  steps {
    script {
        // Adapt the interface and the container IP address for S11 --> SPGW-C
        sh('sed -i -f ci-scripts/adapt-mme-yaml.sed lte/gateway/configs/mme.yml')
        sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make clean"')
        // Re-building w/ S11 enabled
        sh "echo 'make FEATURES=\"mme\" run' > lte/gateway/make_mme_run.sh"
        timeout (time: 15, unit: 'MINUTES') {
          // removing the magma/.cache/gateway folder with speed down build from 3 minutes to 27 minutes
          sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && chmod 755 make_mme_run.sh && sudo chown -R vagrant /home/vagrant/build && ./make_mme_run.sh"')
        }
        sh "sleep 60"
        sh "echo 'make FEATURES=\"mme\" status' > lte/gateway/make_mme_status.sh"
        try {
          // sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && chmod 755 make_mme_status.sh && ./make_mme_status.sh')
          sh('cd lte/gateway && vagrant ssh magma -c "sudo service magma@* status"')
        } catch (Exception e) {
          echo "Status may return an error"
        }
    }
  }
}
stage ("Deploy SPGW-CUPS") {
  steps {
    script {
      sh('cd openair-epc-fed && git clean -ff')
      sh('cd openair-epc-fed && ./scripts/syncComponents.sh --spgwc-branch 2020.w36 --spgwu-tiny-branch 2020.w36')
      // Build containers if they aren't present
      sh('cd openair-epc-fed && ls -l component && docker image inspect oai-spgwc:develop > /dev/null || docker build -f component/oai-spgwc/ci-scripts/Dockerfile.ubuntu18.04 -t oai-spgwc:develop ./component/oai-spgwc')
      sh('cd openair-epc-fed && docker image inspect oai-spgw-tiny:develop > /dev/null || docker build -f component/oai-spgwu-tiny/ci-scripts/Dockerfile.ubuntu18.04 -t oai-spgwu-tiny:develop ./component/oai-spgwu-tiny')
      sh('docker network create --attachable --subnet 192.168.61.128/26 --ip-range 192.168.61.128/26 magma-oai-public-net')
      // We are fixing IP addresses to easy scripting
      sh('docker rm -f magma-oai-spgwc magma-oai-spgwc-tiny || true')
      sh('docker run --privileged --name magma-oai-spgwc --network magma-oai-public-net --ip 192.168.61.130 -d oai-spgwc:develop /bin/bash -c "sleep infinity"')
      sh('docker run --privileged --name magma-oai-spgwu-tiny --network magma-oai-public-net --ip 192.168.61.131 -d oai-spgwu-tiny:develop /bin/bash -c "sleep infinity"')
      // Configure the containers
      sh('cd openair-epc-fed && python3 component/oai-spgwc/ci-scripts/generateConfigFiles.py --kind=SPGW-C --s11c=eth0 --sxc=eth0 --from_docker_file --apn=oai.ipv4')
      sh('cd openair-epc-fed && python3 component/oai-spgwu-tiny/ci-scripts/generateConfigFiles.py --kind=SPGW-U --sxc_ip_addr=192.168.61.130 --sxu=eth0 --s1u=eth0 --from_docker_file')
      sh('cd openair-epc-fed && docker cp ./spgwc-cfg.sh magma-oai-spgwc:/openair-spgwc')
      sh('docker exec -i magma-oai-spgwc /bin/bash -c "cd /openair-spgwc && chmod 777 spgwc-cfg.sh && ./spgwc-cfg.sh"')
      sh('cd openair-epc-fed && docker cp ./spgwu-cfg.sh magma-oai-spgwu-tiny:/openair-spgwu-tiny')
      sh('docker exec -i magma-oai-spgwu-tiny /bin/bash -c "cd /openair-spgwu-tiny && chmod 777 spgwu-cfg.sh && ./spgwu-cfg.sh"')
      // adapting the UE IP pool to magma test setup
      sh('docker cp ./ci-scripts/adapt-spgwc-pool-ip.sed magma-oai-spgwc:/openair-spgwc')
      sh('docker exec -i magma-oai-spgwc /bin/bash -c "sed -i -f adapt-spgwc-pool-ip.sed etc/spgw_c.conf"')
      sh('docker cp ./ci-scripts/adapt-spgwu-pool-ip.sed magma-oai-spgwu-tiny:/openair-spgwu-tiny')
      sh('docker exec -i magma-oai-spgwu-tiny /bin/bash -c "sed -i -f adapt-spgwu-pool-ip.sed etc/spgw_u.conf"')

      // Start cNFs
      sh('docker exec -d magma-oai-spgwc /bin/bash -c "nohup ./bin/oai_spgwc -o -c ./etc/spgw_c.conf > spgwc_check_run.log 2>&1"')
      sh('docker exec -d magma-oai-spgwu-tiny /bin/bash -c "nohup ./bin/oai_spgwu -o -c ./etc/spgw_u.conf > spgwu_check_run.log 2>&1"')
    }
  }
}
stage ("Test-AGW1-w-S11") {
      steps {
        script {
          // making sure the TRF server is up
          echo "Remove unnecessary "
          sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo ip route del 192.168.128.0/24 via 192.168.129.1 dev eth2"')
          sh('cd lte/gateway && vagrant reload magma_test')
          // making sure the TRF server is up
          sh "sleep 60"
          echo "Starting the integration Tests - S1AP Tester"
          timeout (time: 20, unit: 'MINUTES') {
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_detach.py" > ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_detach_multi_ue.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_detach_looped.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_emergency.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_combined_eps_imsi.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_via_guti.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_attach_detach_after_ue_context_release.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
            sh('cd lte/gateway && vagrant ssh magma_test -c "cd magma/lte/gateway/python/integ_tests/ && source ~/build/python/bin/activate && make integ_test TESTS=s1aptests/test_no_auth_response.py" >> ${WORKSPACE}/archives/magma_run_s1ap_tester_s11.log')
          }


          // echo "Stopping the Traffic server in background"
          // sh('cd lte/gateway && vagrant ssh magma_trfserver -c "sudo killall python3"')

          echo "Stopping the SPGW-CUPS"
          sh('docker exec -i magma-oai-spgwc /bin/bash -c "killall --signal SIGINT oai_spgwc || echo oai_spgwc not running"')
          sh('docker exec -i magma-oai-spgwu-tiny /bin/bash -c "killall --signal SIGINT oai_spgwu || echo oai_spgwu not running"')
          sh "sleep 10"
          try {
            sh('docker exec -i magma-oai-spgwc /bin/bash -c "killall --signal SIGKILL oai_spgwc"')
          } catch (Exception e) {
            echo "oai_spgwc may already be killed"
          }
          try {
            sh('docker exec -i magma-oai-spgwu-tiny /bin/bash -c "killall --signal SIGKILL oai_spgwu"')
          } catch (Exception e) {
            echo "oai_spgwu may already be killed"
          }
        }
      }
      post {
        always {
          script {
            sh('cd lte/gateway && vagrant ssh magma -c "cd magma/lte/gateway && make stop"')
            // Retrieving the sys logs and mme log for more debugging.
            sh('cd lte/gateway && vagrant ssh magma -c "sudo cat /var/log/syslog" > ${WORKSPACE}/archives/magma_dev_syslog_s11.log')
            try {
              sh('cd lte/gateway && vagrant ssh magma -c "sudo cat /var/log/mme.log" > ${WORKSPACE}/archives/magma_dev_mme_s11.log')
              sh('docker cp magma-oai-spgwc:/openair-spgwc/spgwc_check_run.log archives')
            } catch (Exception e) {
              echo "MME log may not be available"
            }
            sh('cd lte/gateway && vagrant ssh magma_test -c "sudo cat /var/log/syslog" > ${WORKSPACE}/archives/magma_test_syslog_s11.log')
            // Retrieving the container logs
            sh('docker cp magma-oai-spgwc:/openair-spgwc/spgwc_check_run.log archives')
            sh('docker cp magma-oai-spgwu-tiny:/openair-spgwu-tiny/spgwu_check_run.log archives')
          }
        }
        success {
          sh "echo 'AGW-VM-S1AP-TESTS: OK' >> archives/magma_run_s1ap_tester_s11.log"
        }
        unsuccessful {
          script {
            try {
              sh('docker exec -i magma-oai-spgwc /bin/bash -c "killall --signal SIGKILL oai_spgwc"')
            } catch (Exception e) {
              echo "spgwc may already be stopped"
            }
            try {
              sh('docker exec -i magma-oai-spgwu-tiny /bin/bash -c "killall --signal SIGKILL oai_spgwu"')
            } catch (Exception e) {
              echo "spgwu may already be stopped"
            }
            sh "echo 'AGW-VM-S1AP-TESTS: KO' >> archives/magma_run_s1ap_tester_s11.log"
          }
        }

      }
    }
  }
  post {
    always {
      script {
        sh('git checkout -- lte/gateway/python/integ_tests/defs.mk lte/gateway/configs/mme.yml')

        // Stopping the VMs and the Containers
        sh('cd lte/gateway && vagrant halt magma')
        sh('cd lte/gateway && vagrant halt magma_test')
        sh('cd lte/gateway && vagrant halt magma_trfserver')
        sh('cd lte/gateway && vagrant global-status')

        try {
          sh('docker rm -f magma-oai-spgwc magma-oai-spgwu-tiny')
        } catch (Exception e) {
          echo "We may not have started the CUPS containers"
        }
        try {
          sh('docker network rm magma-oai-public-net')
        } catch (Exception e) {
          echo "We may not have created the CUPS docker network"
        }

        // Generate HTML report
        sh "python3 ci-scripts/generateHtmlReport.py --job_name=${JOB_NAME} --job_id=${BUILD_ID} --job_url=${BUILD_URL} --git_url=${GIT_URL} --git_src_branch=${GIT_BRANCH} --git_src_commit=${GIT_COMMIT}"
        sh "sed -i -e 's#TEMPLATE_TIME#${JOB_TIMESTAMP}#' test_results_magma_converged_mme.html"
        if (fileExists('test_results_magma_converged_mme.html')) {
          archiveArtifacts artifacts: 'test_results_magma_converged_mme.html'
        }

        // Zipping all archived log files
        sh "zip -r -qq magma_logs.zip archives"
        if (fileExists('magma_logs.zip')) {
          archiveArtifacts artifacts: 'magma_logs.zip'
        }
        sh('git stash && git stash clear')
      }
    }
    success {
      script {
          def color = "good"
          def message = "MAGMA " + JOB_NAME + " build (" + BUILD_ID + "): passed (" + BUILD_URL + ")"
          echo message
          sendSocialMediaMessage(slack_channel,color, message)
      }
    }
    unsuccessful {
      script {
          def color = "danger"
          def message = "MAGMA " + JOB_NAME + " build (" + BUILD_ID + "): failed (" + BUILD_URL + ")"
          echo message
          sendSocialMediaMessage(slack_channel,color, message)
      }
    }
  }
}

//-------------------------------------------------------------------------------
// Abstraction function to send social media messages:
// like on Slack or Mattermost
def sendSocialMediaMessage(pipeChannel, pipeColor, pipeMessage) {
    slackSend channel: pipeChannel, color: pipeColor, message: pipeMessage
}

